package com.fsts.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * 
 * 用户登录注册时用户处理与服务器端的链接类
 */
public class UserAction {
	Socket socket = null;
	BufferedReader in = null;
	PrintWriter out = null;
	private String serverIp;
	private int serverPort;

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	// ***********************************
	public UserAction(String serverIp, int serverPort) {
		this.serverIp = serverIp;
		this.serverPort = serverPort;
	}

	/**
	 * 用户注册
	 * 
	 * @param identity
	 * @param userName
	 * @param password
	 * @return
	 */
	public int register(String identity, String name, String password,
			Integer sk) {
		try {
			socket = new Socket(serverIp, serverPort);
			in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(socket
					.getOutputStream()));
			out.println("getTCPublicKey");
			out.flush();
			TimeUnit.SECONDS.sleep(1);
			String key = in.readLine();
			System.out.println(key);
			String temp[] = key.split(",");
			BigInteger n = new BigInteger(temp[0]);
			BigInteger e = new BigInteger(temp[1]);
			BigInteger g = new BigInteger(temp[2]);
			// 计算V值
			BigInteger v = g.pow(sk).modInverse(n);
			v = v.mod(n);
			// 发送注册请求，
			StringBuilder sb = new StringBuilder("register,");
			sb.append(identity).append(",").append(name).append(",").append(
					password).append(",").append(v);
			out.println(sb.toString());
			out.flush();
			TimeUnit.SECONDS.sleep(1);
			String result = in.readLine();
			File file = new File("info.fsts");
			FileWriter fw = new FileWriter(file);
			fw.write(key + "," + result + "," + sk);
			fw.close(); // 写入本地文件中
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 登录检查
	 * 
	 * @param name
	 * @param password
	 * @param ip
	 * @return 0:success<br>
	 *         1:failed, because this userName or password is wrong.<br>
	 *         2:failed, because host error.<br>
	 *         3:failed, IO Exception<br>
	 */
	public int loginCheck(String name, String password) {
		try {
			socket = new Socket(serverIp, serverPort);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
			out.println("login," + name + "," + password);
			out.flush();
			String result = in.readLine();
			if (result.equals("ok")) {
				return 0;
			} else {
				return 1;
			}
		} catch (Exception e) {
			return 2;
		} finally {
			try {
				if(in != null) 
					in.close();
				if(socket != null)
					socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
